﻿using System;
using System.Collections.Generic;
using System.Linq;
using si.caha.Model;
using si.caha.Helper;
using System.Web.Services;
using System.Web.Script.Serialization;

namespace si.caha.Repository
{
    public class ItemRepository : BaseRepository
    {
        public ItemRepository(DataContext dataConext) : base(dataConext){}
        
        public Item Get(int ItemId)
        {
            return GetDataContext().Items.SingleOrDefault(i => i.ItemId == ItemId);
        }

        public List<Item> FilterByCategory(string categoryTitle)
        {
            DataContext dataContext = new DataContext();
            var categoryRepository = new CategoryRepository(dataContext);
            Category category = categoryRepository.GetByTitle(categoryTitle);

            if (category != null)
                return GetAll().Where(i => i.CategoryId == category.CategoryId).ToList();

            return new List<Item>();
        }

        public List<Item> FilterByPrice(int lowerPrice, int upperPrice)
        {
            return GetDataContext().Items.Where(i => i.Price <= upperPrice && i.Price >= lowerPrice).ToList();
        }

        public List<Item> GetAll()
        {
            return GetDataContext().Items.ToList();
        }

        public List<Item> FindByTitleAndDescription(string searchString)
        {
            return GetDataContext().Items.Where(i => i.Title.Contains(searchString) || i.Description.Contains(searchString)).ToList();
        }

        public string GetAllJson()
        {
            var itemTitles = GetDataContext().Items.Select(i => i.Title).ToList();
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            return serializer.Serialize((object)itemTitles);
        }

        public List<Item> GetNewest(int howMany)
        {
            return GetDataContext().Items.OrderByDescending(i => i.DateAdded).Take(howMany).ToList();
        }

        public Dictionary<string, string> Save(Item entity)
        {
            Dictionary<string, string> errors = ValidateAndGetErrors(entity);
            if (errors.Count == 0)
            {
                if (entity.ItemId == 0)
                {
                    entity.DateAdded = System.DateTime.Now;
                    GetDataContext().Items.Add(entity);
                }
                else
                {
                    Item itemToUpdate = GetDataContext().Items.SingleOrDefault(i => i.ItemId == entity.ItemId);
                    //itemToUpdate = entity;
                    itemToUpdate.Title = entity.Title;
                    itemToUpdate.Price = entity.Price;
                    itemToUpdate.Description = entity.Description;
                }
                GetDataContext().SaveChanges();
            }
            return errors;
        }

        public void Delete(int ItemId)
        {
            Item item = GetDataContext().Items.SingleOrDefault(i => i.ItemId == ItemId);
            if (item != null)
            {
                GetDataContext().Items.Remove(item);
                GetDataContext().SaveChanges();
            }
        }
    }
}